<template>
  <div style="width: 100%;height: 264px;box-sizing: border-box;
  padding: 15px;">
    <!-- 读取文档 -->
    <div>
      <input id="uploadFileTempSeat" type="file" hidden @change="afterFileSelected">
      <div style="height: 25px;text-align:left;display: flex;align-items: center;">
        <span style="line-height: 25px;font-weight:400;">读取文档：</span>
        <input class="upload-file-name"
          style="width: 250px;height: 23px;border: 1px solid #333;outline: none;font-size: 12px;border-radius: 5px;"
          :value="uploadFileName">
        <button @click="uploadFileToServer" style="margin-left: 5px;"><i class="iconfont icon-shangchuan"></i></button>
      </div>
      <!-- 间隔时间 -->
      <div style="margin-top: 10px;">
        <span style="line-height: 25px;font-weight: 400;">间隔时间：</span>
        <input type="text" name="" id=""
          style="width: 80px;height: 23px;border: 1px solid #333;outline: none; text-align: center;border-radius: 5px;"
          v-model="timer">
        <span style="margin-left: 5px;">秒</span>
      </div>
      <!-- <div style="margin-top: 10px;"><span style="font-weight: 100;">培训模式：</span><input type="checkbox" name="" id="inp" @click="changedd"></div> -->
      <!-- 注释文档 -->
      <!-- <div style="height: 25px;text-align:left;display: flex;align-items: center;margin-top: 10px;" v-show="flaged">
        <input id="upload" type="file" hidden @change="Selected">
        <span style="line-height: 25px;">注释文档：</span>
        <input class="upload-file-name"
          style="width: 200px;height: 23px;border: 1px solid #333;outline: none;font-size: 12px;" :value="zhuShiName">
        <button @click="zhuShi" style="margin-left: 5px;">上传</button>
      </div> -->
      <div style="position: absolute;bottom: 25px;box-sizing: border-box;padding: 0 0 0 30px;">
        <button @click="beginStart" class="btn">启动</button>
        <button @click="endWrite" class="btn1">暂停</button>
        <button @click="otherScreen" class="btn2">纠错</button>
        <button @click="otherPei" class="btn3">培训</button>
        <!-- <img src="./OtherView.vue" alt=""> -->
      </div>
    </div>
  </div>
</template>
<script >

import { ipcRenderer, clipboard } from 'electron';
import fs from 'fs'
export default {
  components: {
  },
  data() {
    return {
      uploadFileName: '',
      timer: 3,
      // 存放代码
      arr: [],
      //存放注释
      arrs: [],
      stream: [],
      clickFlag: true,
      clickFlag2: false,
      count: 0,
      zhuShiName: '',
      // 控制注释文档是否显示
      flaged: false,
      // peiXun: false,
      // 通过主进程传递到注释的数据
      maxs: {
        count: 0,
        arr1: '',
        arr2: '',
        clear: 0,
      },
      // 通过主进程传递到培训
      peiArr: {
        arr1: '',
        arr2: ''
      }
    }
  },
  watch: {
  },
  mounted() {

  },
  methods: {
    uploadFileToServer: function () {
      const inputElement = document.getElementById('uploadFileTempSeat')
      inputElement.click()
      this.count = 0
    },
    // 选择完文件之后
    afterFileSelected: function (event) {
      const file = event.target.files[0]
      this.uploadFileName = file.path
      this.readDate()
      this.clickFlag2 = true

    },
    // 点击启动
    beginStart() {

      if (this.uploadFileName != '') {
        if (this.arrs.length != 0) {
          this.maxs.clear = 1
          ipcRenderer.send('create-zhu', JSON.stringify(this.maxs))
        }
        this.arr = []
        this.arrs = []
        // const currentWindow = remote.getCurrentWindow()
        const electron = require('electron');
        const remote = electron.remote;
        const currentWindow = remote.getCurrentWindow();
        currentWindow.minimize()
        // 监听窗口最小化事件
        currentWindow.on('minimize', () => {
          console.log('应用程序最小化');
          this.clickFlag =true
        });
        // 监听窗口还原事件
        currentWindow.on('restore', () => {
          this.clickFlag =false
          console.log('应用程序还原');
        });
        // 打开软件
        // this.openRu()
        // 读取
        setTimeout(() => {
          this.readDate()
          // 写入
          this.copyArrayToClipboard()
        }, 1000)

      } else {
        alert('请选择文件')
      }


    },
    // 打开
    // openRu() {
    //   let robot = require("robotjs");
    //   // 鼠标移动到某处
    //   robot.moveMouseSmooth(373,38);
    //   robot.mouseClick();
    //   robot.moveMouseSmooth(442,130);
    //   robot.mouseClick()
    //   for (let i = 0; i < 3; i++) {
    //     robot.keyTap('enter');
    //   }
    // },
    // 读取
    readDate() {
      this.arr = []
      this.arrs = []
      // 使用 fs 模块进行文件操作
      try {
        const data = fs.readFileSync(`${this.uploadFileName}`, 'utf-8');
        const lines = data.split(/\r?\n/);
        lines.forEach((line) => {
          if (!line.match(/^\/\//)&&line!='') {
            this.arr.push(`${line}`)
            // 存到vuex
            this.maxs.arr1 = this.arr
            this.peiArr.arr1 = this.arr
          }
          if (line.match(/^\/\//)&&line!='') {
            // this.peiXun = true
            this.arrs.push(`${line.slice(2)}`)
            this.maxs.arr2 = this.arrs
            this.peiArr.arr2 = this.arrs
            console.log(this.arrs);
          }
        });
      } catch (err) {
        console.error(err);
      }

    },
    // 复制黏贴
    copyArrayToClipboard() {
      let robot = require("robotjs");
      // 将数组转换为字符串
      const writeLine = () => {
        if (this.clickFlag == true) {
          // 首先打开注释窗体
          if (this.arrs.length != 0&&this.arrs.length != 0) {
            ipcRenderer.send('create-zhu')
          }
          // 复制
          robot.keyTap('c', ['control']);
          clipboard.writeText(this.arr[this.count] + '\n');
          // 粘贴
          robot.keyTap('v', ['control']);
          this.count = this.count + 1;
          // 向主进程传递参数
          if (this.arrs != []) {
            this.maxs.count = this.count
            if (this.arrs.length != 0) {
              if (this.clickFlag2 == true) {
                this.maxs.clear = 1
                ipcRenderer.send('create-zhu', JSON.stringify(this.maxs))
                this.clickFlag2 = false
              }
              if (this.clickFlag2 == false) {
                this.maxs.clear = 2
                ipcRenderer.send('create-zhu', JSON.stringify(this.maxs))
              }
            }
          }
          if (this.count < this.arr.length) {
            // 回调
            setTimeout(writeLine, this.timer + '000');
          } else {
            this.count = 0
          }
        }
      }
      writeLine()
    },
    // 暂停写入
    endWrite() {
      alert('自动配置已终止')
      ipcRenderer.send('close-window')
      this.count = 0
      this.arr = [],
        this.arrs = []

    },
    // 纠错
    otherScreen() {
      localStorage.setItem('text', this.uploadFileName)
      ipcRenderer.send('create-new-window')
    },
    // 培训
    otherPei() {
      if (this.uploadFileName) {
        this.readDate()
        //  ipcRenderer.send('create-new-pei')
        if (this.arrs.length > 0) {
          ipcRenderer.send('create-new-pei', {
            windowId: 2,
            data: JSON.stringify(this.peiArr)
          })
        } else {
          alert('请输入带有注释的代码')
        }

      } else {
        alert('请选择文件')
      }

    },
  }
}
</script>
<style >
.upload-file-name {
  width: 50px;
  height: 20px
}

.btn,
.btn1,
.btn2,
.btn3 {
  width: 55.11px;
  height: 25px;
  margin-left: 30px;
  border-radius: 5px;
  font-size: 12px;
  color: #fff;
  background-color: #6C8FF8;
  border: 2px solid #6C8FF8;
}
</style>
